﻿using System;
using System.Collections.ObjectModel;
using Unknown.Framework.Database;
using Unknown.Framework.Injection;
using Unknown.Source.Entity.Example;
using Unknown.Source.Sql.Example;

namespace Unknown.Source.Access.Example
{
	/// <summary>
	/// 艺术品访问抽象类
	/// </summary>
	public abstract class ArtworkAccess
	{
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>结果</returns>
        public virtual Artwork ExecuteInsert(Artwork entity)
        {
            Artwork result = null;

			if (entity != null)
			{
				DatabaseHandler databaseHandler = new DatabaseHandler();
				IArtworkSqlCustom sql = ObjectFactory.Build(typeof(IArtworkSqlCustom)) as IArtworkSqlCustom;
				if (sql != null)
				{
					entity.Pkid = DatabaseHandler.BuildIdentify(entity.Pkid);
					Operation operation = sql.BuildInsert(entity);
					if (operation != null)
					{
						NoQueryOperationHandler operationHandler = operation.OperationHandler as NoQueryOperationHandler;
						if (operationHandler != null)
						{
							databaseHandler.Execute(operation);
							if (operationHandler.Result > 0)
							{
								result = entity;
							}
						}
					}
				}
			}

            return result;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>结果</returns>
        public virtual int ExecuteUpdate(Artwork entity)
        {
            int result = 0;

			if (entity != null)
			{
				DatabaseHandler databaseHandler = new DatabaseHandler();
				IArtworkSqlCustom sql = ObjectFactory.Build(typeof(IArtworkSqlCustom)) as IArtworkSqlCustom;
				if (sql != null)
				{
					Operation operation = sql.BuildUpdate(entity);
					if (operation != null)
					{
						NoQueryOperationHandler operationHandler = operation.OperationHandler as NoQueryOperationHandler;
						if (operationHandler != null)
						{
							databaseHandler.Execute(operation);
							result = operationHandler.Result;
						}
					}
				}
			}

            return result;
        }

        /// <summary>
        /// 删除
        /// </summary>
		/// <param name="pkid">标识</param>
        /// <returns>结果</returns>
        public virtual int ExecuteDelete(string pkid)
        {
            int result = 0;

            DatabaseHandler databaseHandler = new DatabaseHandler();
            IArtworkSqlCustom sql = ObjectFactory.Build(typeof(IArtworkSqlCustom)) as IArtworkSqlCustom;
            if (sql != null)
            {
                Operation operation = sql.BuildDelete(pkid);
                if (operation != null)
                {
                    NoQueryOperationHandler operationHandler = operation.OperationHandler as NoQueryOperationHandler;
                    if (operationHandler != null)
                    {
                        databaseHandler.Execute(operation);
                        result = operationHandler.Result;
                    }
                }
            }

            return result;
        }

		/// <summary>
		/// 查询
		/// </summary>
		/// <param name="first">开始行</param>
		/// <param name="last">结束行</param>
		/// <returns>结果</returns>
		public virtual Collection<Artwork> ExecuteSelect(int first, int last)
		{
			Collection<Artwork> results = null;

			DatabaseHandler databaseHandler = new DatabaseHandler();
			IArtworkSqlCustom sql = ObjectFactory.Build(typeof(IArtworkSqlCustom)) as IArtworkSqlCustom;
			if (sql != null)
			{
				Operation operation = sql.BuildSelect(first, last);
				if (operation != null)
				{
					AbstractReaderOperationHandler<Artwork> operationHandler = operation.OperationHandler as AbstractReaderOperationHandler<Artwork>;
					if (operationHandler != null)
					{
						databaseHandler.Execute(operation);
						results = operationHandler.Results;
					}
				}
			}

			return results;
		}

        /// <summary>
        /// 查询
        /// </summary>
		/// <param name="pkid">标识</param>
        /// <returns>结果</returns>
        public virtual Artwork ExecuteSelect(string pkid)
        {
            Artwork result = null;

            DatabaseHandler databaseHandler = new DatabaseHandler();
            IArtworkSqlCustom sql = ObjectFactory.Build(typeof(IArtworkSqlCustom)) as IArtworkSqlCustom;
            if (sql != null)
            {
                Operation operation = sql.BuildSelect(pkid);
                if (operation != null)
                {
                    AbstractReaderOperationHandler<Artwork> operationHandler = operation.OperationHandler as AbstractReaderOperationHandler<Artwork>;
                    if (operationHandler != null)
                    {
                        databaseHandler.Execute(operation);
                        if ((operationHandler.Results != null) && (operationHandler.Results.Count == 1))
                        {
                            result = operationHandler.Results[0];
                        }
                    }
                }
            }

            return result;
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>结果</returns>
        public virtual Artwork ExecuteSelectUnique(Artwork entity)
        {
            Artwork result = null;

			if (entity != null)
			{
				DatabaseHandler databaseHandler = new DatabaseHandler();
				IArtworkSqlCustom sql = ObjectFactory.Build(typeof(IArtworkSqlCustom)) as IArtworkSqlCustom;
				if (sql != null)
				{
					Operation operation = sql.BuildSelectUnique(entity);
					if (operation != null)
					{
						AbstractReaderOperationHandler<Artwork> operationHandler = operation.OperationHandler as AbstractReaderOperationHandler<Artwork>;
						if (operationHandler != null)
						{
							databaseHandler.Execute(operation);
							if ((operationHandler.Results != null) && (operationHandler.Results.Count == 1))
							{
								result = operationHandler.Results[0];
							}
						}
					}
				}
            }

            return result;
        }
	}
}
